package com.zbkj.crmeb.store.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.crmeb.chant.model.CategoryMatch;
import com.zbkj.crmeb.chant.service.ICategoryMatchService;
import com.zbkj.crmeb.store.model.ProductInformation;
import com.zbkj.crmeb.store.response.StoreProductRuleInformationResponse;
import com.zbkj.crmeb.store.service.IProductInformationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageHelper;
import com.zbkj.crmeb.store.dao.StoreProductRuleDao;
import com.zbkj.crmeb.store.model.StoreProductRule;
import com.zbkj.crmeb.store.request.StoreProductRuleRequest;
import com.zbkj.crmeb.store.request.StoreProductRuleSearchRequest;
import com.zbkj.crmeb.store.service.StoreProductRuleService;

/**
* @author Mr.Zhang
* @description StoreProductRuleServiceImpl 接口实现
* @date 2020-05-27
*/
@Service
public class StoreProductRuleServiceImpl extends ServiceImpl<StoreProductRuleDao, StoreProductRule> implements StoreProductRuleService {

    @Resource
    private StoreProductRuleDao dao;
    @Autowired
    private ICategoryMatchService categoryMatchService;
    @Autowired
    private IProductInformationService productInformationService;


    /**
    * 列表
    * @param request 请求参数
    * @param pageParamRequest 分页类参数
    * @author Mr.Zhang
    * @since 2020-05-27
    * @return List<StoreProductRule>
    */
    @Override
    public List<StoreProductRule> getList(StoreProductRuleSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        //带 StoreProductRule 类的多条件查询
        LambdaQueryWrapper<StoreProductRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(null != request.getKeywords()){
            lambdaQueryWrapper.like(StoreProductRule::getRuleName, request.getKeywords());
            lambdaQueryWrapper.or().like(StoreProductRule::getRuleValue, request.getKeywords());
        }
        if(StringUtils.isNotBlank(request.getMerId())){
            lambdaQueryWrapper.eq(StoreProductRule::getMerId,request.getMerId());
        }
        lambdaQueryWrapper.orderByDesc(StoreProductRule::getId);
        return dao.selectList(lambdaQueryWrapper);
    }

    @Override
    public StoreProductRuleInformationResponse getRuleByCate(StoreProductRuleSearchRequest request) {
        //获取分类对应的规格，为空时取上一级数据
        String productRuleIds = "";
        String informationIds = "";
        if (Objects.nonNull(request.getCateId())){
            CategoryMatch categoryMatch = categoryMatchService.getById(request.getCateId());
            if(categoryMatch != null){
                productRuleIds = categoryMatch.getProductRuleIds();
                informationIds = categoryMatch.getInformationIds();
                if(StringUtils.isBlank(productRuleIds) || StringUtils.isBlank(informationIds)){
                    categoryMatch = categoryMatchService.getById(categoryMatch.getPid());
                    if(categoryMatch != null){
                        productRuleIds = StringUtils.isBlank(productRuleIds) ? categoryMatch.getProductRuleIds() : productRuleIds;
                        informationIds = StringUtils.isBlank(informationIds) ? categoryMatch.getInformationIds() : informationIds;
                        if(StringUtils.isBlank(productRuleIds) || StringUtils.isBlank(informationIds)){
                            categoryMatch = categoryMatchService.getById(categoryMatch.getPid());
                            if(categoryMatch != null){
                                productRuleIds = StringUtils.isBlank(productRuleIds) ? categoryMatch.getProductRuleIds() : productRuleIds;
                                informationIds = StringUtils.isBlank(informationIds) ? categoryMatch.getInformationIds() : informationIds;
                            }
                        }
                    }
                }
            }
        }
        //带 StoreProductRule 类的多条件查询
        LambdaQueryWrapper<StoreProductRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(null != request.getKeywords()){
            lambdaQueryWrapper.like(StoreProductRule::getRuleName, request.getKeywords());
            lambdaQueryWrapper.or().like(StoreProductRule::getRuleValue, request.getKeywords());
        }
        if(StringUtils.isNotBlank(request.getMerId())){
            lambdaQueryWrapper.eq(StoreProductRule::getMerId,request.getMerId());
        }
        if (StringUtils.isNotBlank(productRuleIds)){
            List<Integer> ruleIds = Convert.toList(Integer.class,productRuleIds.split(","));
            lambdaQueryWrapper.in(StoreProductRule::getId,ruleIds);
        }
        lambdaQueryWrapper.orderByDesc(StoreProductRule::getId);
        StoreProductRuleInformationResponse response = new StoreProductRuleInformationResponse();
        response.setStoreProductRuleList(dao.selectList(lambdaQueryWrapper));
        if (StringUtils.isNotBlank(informationIds)){
            List<Integer> ids = Convert.toList(Integer.class,informationIds.split(","));
            List<ProductInformation> informationList = productInformationService.list(Wrappers.lambdaQuery(ProductInformation.class).in(ProductInformation::getId,ids));
            response.setInformationList(informationList);
        }
        return response;
    }

    /**
     * 新增商品规格
     * @param storeProductRuleRequest 规格参数
     * @return 新增结果
     */
    @Override
    public boolean save(StoreProductRuleRequest storeProductRuleRequest) {
        if(getListByRuleName(storeProductRuleRequest.getRuleName()).size() > 0){
            throw new CrmebException("此规格值已经存在");
        }
        StoreProductRule storeProductRule = new StoreProductRule();
        BeanUtils.copyProperties(storeProductRuleRequest, storeProductRule);
        return save(storeProductRule);
    }

    /**
     * 根据规格名称查询同名规格
     * @param ruleName 规格名称
     * @return 查询到的数据
     */
    private List<StoreProductRule> getListByRuleName(String ruleName){
        LambdaQueryWrapper<StoreProductRule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isBlank(ruleName)){
            return new ArrayList<>();
        }
            lambdaQueryWrapper.eq(StoreProductRule::getRuleName, ruleName);
        return dao.selectList(lambdaQueryWrapper);
    }
}

